package boerse.main.StockPriceProvider;

import boerse.main.Asset.Share;
import boerse.main.Exceptions.NoSuchShareException;
import boerse.main.Gui.ViewerDemo;
import java.util.Timer;

public abstract class StockPriceProvider implements StockPriceInfo {

	protected Share[] allShare = {new Share("bmw", 250), new Share("kuka", 200), new Share("mercedes", 219),
							new Share("nestle", 123), new Share("fca", 50), new Share("fcb", 150),
                            new Share("vfb", 100), new Share("bvb", 120)};
    protected Timer timer;
	
	public StockPriceProvider() {
		super();
        timer = MyTimer.getTimer();
	}
	
	@Override
	public boolean isShareListed(String shareName) {
		try {
			searchShare(shareName);
			return true;
		} catch (NoSuchShareException e) {
			return false;
		}
		
	}

    public Timer getTime() {
        return timer;
    }

	@Override
	public long getCurrentShareRate(String shareName) {
		
		return searchShare(shareName).getPrice();
	}

    public void showTicker() {
        ViewerDemo viewer = new ViewerDemo(allShare);
        viewer.start(timer);
    }

	@Override
	public Share[] getAllShares() {
		return allShare;
	}
	
	public Share getShare(String shareName) {
		
		return searchShare(shareName);
	}
	
	protected abstract void updateShareRates();
	
	protected abstract void updateShareRate(String shareName);
	
	public abstract void startUpdate();
	
	
	private Share searchShare(String shareName) {
				
		for (int i = 0; i < allShare.length; i++) {
			if (allShare[i] == null)
				break;
			if (shareName.equals(allShare[i].getName())) {
				return allShare[i];
			}
		}
		
		throw new NoSuchShareException();
	}

}
